import 'package:flutter/material.dart';
import 'package:seedfour/app/theme.dart';
import 'package:seedfour/app/views/module_case/case_detail/state.dart';

import '../../../../constants.dart';

class CaseInfoWidget extends StatelessWidget {
  const CaseInfoWidget({
    Key? key,
    required this.data,
  }) : super(key: key);

  final CaseDetailState data;

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.symmetric(horizontal: kSpaceSmall),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(kRadiusMedium),
        color: Colors.white,
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Padding(
            padding: EdgeInsets.all(kSpaceSmall),
            child: Text(
              '基本情况',
              style: TextStyle(
                fontSize: kFontSizeMedium,
                color: AppColor.colorFontBlack,
                fontWeight: AppFont.fontMedium,
              ),
            ),
          ),
          const Divider(),
          _buildCaseItemBg(builder: (item) {
            return [
              Padding(
                padding: EdgeInsets.symmetric(
                    horizontal: kSpaceMedium, vertical: kSpaceSmall),
                child: _buildInfoItem(item: item),
              ),
            ];
          }),
          _buildPersonItemBg(builder: (item) {
            return [
              Padding(
                padding: EdgeInsets.symmetric(
                    horizontal: kSpaceMedium, vertical: kSpaceMini),
                child: _buildPersonItem(item: item),
              ),
            ];
          }),
          SizedBox(height: kSpaceSmall),
        ],
      ),
    );
  }

  Widget _buildInfoItem({required CaseDetailSubItemInfo item}) {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Text(
          item.title,
          style: TextStyle(
            fontSize: kFontSizeSmall,
            color: AppColor.colorFontBlack,
          ),
        ),
        SizedBox(width: kSpaceSmall),
        Expanded(child:  Text(
          textAlign: TextAlign.end,
          item.content,
          style: TextStyle(
            fontSize: kFontSizeSmall,
            color: item.contentColor,
          ),
        ),),
      ],
    );
  }

  Widget _buildPersonItem({required CaseDetailItemInfo item}) {
    return Container(
      width: double.maxFinite,
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(kRadiusMedium),
        color: AppColor.colorRectangleGrey,
      ),
      child: item.subItemList.isEmpty
          ? Padding(
              padding: EdgeInsets.all(kSpaceMedium),
              child: Text(
                '无${item.title}',
                style: TextStyle(
                  fontSize: kFontSizeMedium,
                  color: AppColor.colorFontBlack,
                  fontWeight: AppFont.fontMedium,
                ),
              ),
            )
          : Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisAlignment: MainAxisAlignment.start,
              children: [
                Padding(
                  padding: EdgeInsets.all(kSpaceMedium),
                  child: Text(
                    item.title,
                    style: TextStyle(
                      fontSize: kFontSizeMedium,
                      color: AppColor.colorFontBlack,
                      fontWeight: AppFont.fontMedium,
                    ),
                  ),
                ),
                const Divider(color: Color(0xFFE3E6EB)),
                SizedBox(height: kSpaceSmall),
                ...List.generate(
                  item.subItemList.length,
                  (index) => Padding(
                    padding: EdgeInsets.symmetric(
                        horizontal: kSpaceMedium, vertical: kSpaceTiny),
                    child: _buildInfoItem(item: item.subItemList[index]),
                  ),
                ),
                SizedBox(height: kSpaceSmall),
              ],
            ),
    );
  }

  Widget _buildCaseItemBg({
    required List<Widget> Function(CaseDetailSubItemInfo item) builder,
  }) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: List.generate(
        data.caseInfoList.length,
        (index) => Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: builder(data.caseInfoList[index]),
        ),
      ),
    );
  }

  Widget _buildPersonItemBg({
    required List<Widget> Function(CaseDetailItemInfo item) builder,
  }) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: List.generate(
        data.casePersonList.length,
        (index) => Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: builder(data.casePersonList[index]),
        ),
      ),
    );
  }
}
